home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Libraries / continued_fraction / continued_fraction.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-08  |  1.4 KB  |  66 lines  |  [TEXT/KAHL]

  1. class continued_fraction
  2. {
  3.     public:
  4.         continued_fraction( long double number);
  5.         
  6.         void restart();
  7.         //
  8.         // step returns the number of steps actually taken. As soon as 'rest'
  9.         // becomes zero further steps do not change the approximation. This
  10.         // can be because the approximation has become exact, or because it
  11.         // has become exteremely good.
  12.         //
  13.         int step( int numsteps = 1);
  14.         long double step_till_relerror( long double max_relerror);
  15.         long double step_till_error( long double max_error);
  16.  
  17.         friend ostream &operator<<( ostream &os, const continued_fraction &it);
  18.  
  19.         long double get_error() const;
  20.         long double get_relerror() const;
  21.         unsigned long numerator() const;
  22.         unsigned long denumerator() const;
  23.         
  24.     private:
  25.         const long double x;
  26.         long double rest;
  27.         unsigned long p_1;
  28.         unsigned long p_2;
  29.         unsigned long q_1;
  30.         unsigned long q_2;
  31.  
  32.         long double fract;
  33.         long double error;
  34.         long double relerror;
  35.         
  36.         Boolean step_once();
  37.         void compute_deriveds();
  38. };
  39.  
  40. inline void continued_fraction::compute_deriveds()
  41. {
  42.     fract    = (long double) p_1 / (long double) q_1;
  43.     error    = fabs( x - fract);
  44.     relerror = error / x;
  45. }
  46.  
  47. inline long double continued_fraction::get_error() const
  48. {
  49.     return error;
  50. }
  51.  
  52. inline long double continued_fraction::get_relerror() const
  53. {
  54.     return relerror;
  55. }
  56.  
  57. inline unsigned long continued_fraction::numerator() const
  58. {
  59.     return p_1;
  60. }
  61.  
  62. inline unsigned long continued_fraction::denumerator() const
  63. {
  64.     return q_1;
  65. }
  66.